package com.ruoyi.labsafety.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.labdata.domain.LabFile;
import com.ruoyi.labsafety.domain.LabSafetyCheckTaskIndexItem;
import com.ruoyi.labsafety.service.ILabSafetyCheckTaskIndexItemService;

/**
 * 安检检查任务指标项Controller
 * 
 * @author jun.wang
 * @date 2022-04-25
 */
@RestController
@RequestMapping("/labsafety/taskItem")
public class LabSafetyCheckTaskIndexItemController extends BaseController
{
    @Autowired
    private ILabSafetyCheckTaskIndexItemService labSafetyCheckTaskIndexItemService;

    /**
     * 查询安检检查任务指标项列表
     */
//    @PreAuthorize("@ss.hasPermi('labsafety:taskItem:list')")
    @GetMapping("/list")
    public TableDataInfo list(LabSafetyCheckTaskIndexItem labSafetyCheckTaskIndexItem)
    {
        startPage();
        List<LabSafetyCheckTaskIndexItem> list = labSafetyCheckTaskIndexItemService.selectLabSafetyCheckTaskIndexItemList(labSafetyCheckTaskIndexItem);
        return getDataTable(list);
    }

    /**
     * 导出安检检查任务指标项列表
     */
//    @PreAuthorize("@ss.hasPermi('labsafety:taskItem:export')")
    @Log(title = "安检检查任务指标项", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(LabSafetyCheckTaskIndexItem labSafetyCheckTaskIndexItem)
    {
        List<LabSafetyCheckTaskIndexItem> list = labSafetyCheckTaskIndexItemService.selectLabSafetyCheckTaskIndexItemList(labSafetyCheckTaskIndexItem);
        ExcelUtil<LabSafetyCheckTaskIndexItem> util = new ExcelUtil<LabSafetyCheckTaskIndexItem>(LabSafetyCheckTaskIndexItem.class);
        return util.exportExcel(list, "taskItem");
    }

    /**
     * 获取安检检查任务指标项详细信息
     */
//    @PreAuthorize("@ss.hasPermi('labsafety:taskItem:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(labSafetyCheckTaskIndexItemService.selectLabSafetyCheckTaskIndexItemById(id));
    }

    /**
     * 新增安检检查任务指标项
     */
//    @PreAuthorize("@ss.hasPermi('labsafety:taskItem:add')")
    @Log(title = "安检检查任务指标项", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LabSafetyCheckTaskIndexItem labSafetyCheckTaskIndexItem)
    {
        return toAjax(labSafetyCheckTaskIndexItemService.insertLabSafetyCheckTaskIndexItem(labSafetyCheckTaskIndexItem));
    }

    /**
     * 修改安检检查任务指标项
     */
//    @PreAuthorize("@ss.hasPermi('labsafety:taskItem:edit')")
    @Log(title = "安检检查任务指标项", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LabSafetyCheckTaskIndexItem labSafetyCheckTaskIndexItem)
    {
        return toAjax(labSafetyCheckTaskIndexItemService.updateLabSafetyCheckTaskIndexItem(labSafetyCheckTaskIndexItem));
    }

    /**
     * 删除安检检查任务指标项
     */
//    @PreAuthorize("@ss.hasPermi('labsafety:taskItem:remove')")
    @Log(title = "安检检查任务指标项", businessType = BusinessType.DELETE)
	  @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(labSafetyCheckTaskIndexItemService.deleteLabSafetyCheckTaskIndexItemByIds(ids));
    }

    //    @PreAuthorize("@ss.hasPermi('labsafety:item:edit')")
    @Log(title = "安检检查指标项", businessType = BusinessType.UPDATE)
    @PutMapping("/status")
    public AjaxResult updateStatus(@RequestBody LabSafetyCheckTaskIndexItem taskIndexItem)
    {
        return toAjax(labSafetyCheckTaskIndexItemService.updateLabSafetyCheckTaskIndexItemStatus(taskIndexItem));
    }

    /**
     * 获取安检检查任务指标项详细信息
     */
    @GetMapping(value = "/getTreeTableInfo/{instanceId}")
    public TableDataInfo getTreeTableInfo(@PathVariable("instanceId") Long instanceId)
    {
        List<LabSafetyCheckTaskIndexItem> list = labSafetyCheckTaskIndexItemService.getTreeTableInfo(instanceId);
        return getDataTable(list);
    }

    //    @PreAuthorize("@ss.hasPermi('labsafety:item:edit')")
    @Log(title = "安检检查指标项", businessType = BusinessType.UPDATE)
    @PutMapping("/hiddenDanger")
    public AjaxResult updateHiddenDanger(@RequestBody LabSafetyCheckTaskIndexItem taskIndexItem)
    {
        return toAjax(labSafetyCheckTaskIndexItemService.updateLabSafetyCheckTaskIndexItemHiddenDanger(taskIndexItem));
    }

    @Log(title = "安检检查指标项", businessType = BusinessType.UPDATE)
    @PostMapping("/updateHiddenDanger")
    public AjaxResult updateHiddenDangerStatus(@RequestBody LabSafetyCheckTaskIndexItem taskIndexItem)
    {
        return toAjax(labSafetyCheckTaskIndexItemService.updateLabSafetyCheckTaskIndexItemHiddenDanger(taskIndexItem));
    }
    /**
     * 新增安检检查任务指标项
     */
//    @PreAuthorize("@ss.hasPermi('labsafety:taskItem:add')")
    @Log(title = "安检检查任务指标项", businessType = BusinessType.INSERT)
    @PostMapping("file")
    public AjaxResult file(@RequestBody LabSafetyCheckTaskIndexItem labSafetyCheckTaskIndexItem)
    {
        return toAjax(labSafetyCheckTaskIndexItemService.insertLabSafetyCheckTaskIndexItemFile(labSafetyCheckTaskIndexItem));
    }
    /**
     * 新增安检检查任务指标项
     */
//    @PreAuthorize("@ss.hasPermi('labsafety:taskItem:add')")
    @Log(title = "安检检查任务指标项", businessType = BusinessType.INSERT)
    @GetMapping("file/{id}")
    public AjaxResult getFile(@PathVariable("id") Long id)
    {
    	LabSafetyCheckTaskIndexItem taskItem = labSafetyCheckTaskIndexItemService.selectLabSafetyCheckTaskIndexItemById(id);
    	if(StringUtils.isEmpty(taskItem.getName())){
    		List<LabSafetyCheckTaskIndexItem> parentList = labSafetyCheckTaskIndexItemService.selectLabSafetyCheckTaskIndexItemByNo(taskItem.getItemNo());
    		if(parentList.size()>0){
    			taskItem.setName(parentList.get(0).getName());
    		}
    		
    	}
    	List<LabFile> allFiles = labSafetyCheckTaskIndexItemService.selectLabSafetyCheckTaskIndexItemFileById(id);
    	taskItem.setFiles(allFiles);
    	return AjaxResult.success(taskItem);
    }

    //@PreAuthorize("@ss.hasPermi('labsafety:item:edit')")
    @Log(title = "安检检查指标项", businessType = BusinessType.UPDATE)
    @PutMapping("/syncStatus")
    public AjaxResult updateSyncStatus(@RequestBody LabSafetyCheckTaskIndexItem taskIndexItem)
    {
        return toAjax(labSafetyCheckTaskIndexItemService.updateLabSafetyCheckTaskIndexItemSyncStatus(taskIndexItem));
    }


}
